<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .slider-bar {
            position: absolute;
            left: 50%;
            top: 300px;
            margin-left: 600px;
            width: 45px;
            height: 130px;
            background-color: pink;
        }

        .w {
            width: 1200px;
            margin: 10px auto;
        }

        .header {
            height: 150px;
            background-color: purple;
        }

        .banner {
            height: 250px;
            background-color: skyblue;
        }

        .main {
            height: 1000px;
            background-color: yellowgreen;
        }

        span {
            display: none;
            position: absolute;
            bottom: 0;
        }
    </style>
</head>

<body>
    <div class="slider-bar">
        <span class="goBack">返回顶部</span>
    </div>
    <div class="header w">头部区域</div>
    <div class="banner w">banner区域</div>
    <div class="main w">主体部分</div>
    <script>
        // 获取元素
        const slider_bar = document.querySelector('.slider-bar');
        const slider_barTop = slider_bar.offsetTop;
        const goBack = document.querySelector('.goBack');
        const banner = document.querySelector('.banner');
        const bannerTop = banner.offsetTop;
        const main = document.querySelector('.main');
        const mainTop = main.offsetTop;

        // 页面滚动
        document.addEventListener('scroll', function (e) {
            let top = window.pageYOffset;
            // let top = document.documentElement.scrollTop;

            // 侧栏固定
            if (top > bannerTop) {
                slider_bar.style.top = slider_barTop - bannerTop + 'px';
                slider_bar.style.position = 'fixed';
            } else {
                slider_bar.style.position = 'absolute';
                slider_bar.style.top = '300px';
            }

            // 显示返回顶部
            if (top > mainTop) {
                goBack.style.display = 'block';
            } else {
                goBack.style.display = 'none';
            }

            // 滑动返回顶部
            goBack.addEventListener('click', function () {
                scrollTo(window, 0);
            })

            // 滑动滚动函数
            function scrollTo(obj, target, callback) {
                clearInterval(obj.timer);
                obj.timer = setInterval(function() {
                    let step = (target - window.pageYOffset) / 10;
                    step = step > 0 ? Math.ceil(step) : Math.floor(step);
                    if (window.pageYOffset == target) {
                        clearInterval(obj.timer);
                        callback && callback();
                    }
                    window.scroll(0, window.pageYOffset + step);
                }, 15);
            }
        });
    </script>
</body>

</html>